package com.project.weimi.activity;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.xutils.x;

import com.project.weimi.R;
import com.project.weimi.application.Constant;
import com.project.weimi.entity.response.ResponseBean;
import com.project.weimi.interfaces.MyCallBack;
import com.project.weimi.utils.AgeUtil;
import com.project.weimi.utils.GsonUtil;
import com.project.weimi.utils.NetUtils;
import com.project.weimi.utils.XUtil;
import com.project.weimi.widget.ToastMessage;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
/**
 * 年龄
 * @author Administrator
 *
 */
public class AgeActivity extends BaseActivity{

	private TextView tv_age,tv_constellation;
	private ToggleButton btn_toggle;
	private DatePicker datePicker;
	// 根据生日计算出的年龄
	private int age;
	// 根据生日计算出的星座
	private String starSeat;
	private String userPhone;
	String mAge;
	
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentViewWithBar(R.layout.activity_age, "出生日期", true, "保存");
		x.view().inject(this);
		// 获得从个人信息页面传来的年龄值
		mAge = getIntent().getStringExtra("age");
		
		initView();
		
	}
	
	private void initView(){
		tv_age = (TextView)findViewById(R.id.tv_age);
		tv_constellation = (TextView)findViewById(R.id.tv_constellation);
		btn_toggle = (ToggleButton)findViewById(R.id.btn_toggle);
		datePicker = (DatePicker)findViewById(R.id.datePicker);
		
		if(TextUtils.isEmpty(mAge)){
			tv_age.setText("0");
		} else {
			tv_age.setText(mAge);
		}
		
		datePicker.setCalendarViewShown(false);
		Calendar calendar = Calendar.getInstance();
		int y = calendar.get(Calendar.YEAR);
        int m = calendar.get(Calendar.MONTH);
        int d = calendar.get(Calendar.DAY_OF_MONTH);
		datePicker.init(y, m, d, onDateChangedListener);
		
//		int y2 = datePicker.getYear();  
//		int m2 = datePicker.getMonth()+1;  
//		int d2 = datePicker.getDayOfMonth();  
//		Log.i("aaa", "年："+y2+";月:"+m2+";日:"+d2);
		
		// 监听
		btn_toggle.setOnCheckedChangeListener(checkedChangeListener);
		bar.getRightBar().setOnClickListener(onClickListener);
	}
	
	private OnDateChangedListener onDateChangedListener = new OnDateChangedListener() {
		
		@Override
		public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			String str = year + "-" + (monthOfYear+1) + "-" + dayOfMonth;
			
			Date date = null;
			try {
				date = AgeUtil.converToDate(str);
				// 得到年龄
				age = AgeUtil.getAge(date);
				tv_age.setText(age + "");
				// 得到星座
				starSeat = AgeUtil.getStarSeat((monthOfYear+1), dayOfMonth);
				tv_constellation.setText(starSeat);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};
	
	private OnClickListener onClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			if(!NetUtils.isConnected(AgeActivity.this)){
				ToastMessage.showToast(AgeActivity.this, "网络不给力，请检查网络设置");
			} else {
				initHttp();
			}
			
		}
	};
	
	private void initHttp(){
		String strAge = tv_age.getText().toString().trim();
		
		if(strAge.equals(mAge)){
			ToastMessage.showMyToast(AgeActivity.this, "年龄与原来保持相同，未做修改");
			finish();
			return;
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberId", userPhone);
		map.put("age", age);
		
		//Log.e("TAG", "保存的年龄==="+age);
		
		XUtil.post(Constant.CHANGE_AGE, map, new MyCallBack<String>(){
			
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				if(null != result && !"".equals(result)){
					//Log.e("TAG", "AgeActivity==="+result);
					ResponseBean bean = (ResponseBean) GsonUtil.jsonToBean(
							result, ResponseBean.class);
					if(null != bean){
						String str = bean.getResult();
						if(!TextUtils.isEmpty(str) && "1".equals(str)){
							ToastMessage.showMyToast(AgeActivity.this, "修改成功");
							Intent intent = new Intent();
							intent.putExtra("age", String.valueOf(age));
							setResult(2, intent);
							finish();
						}
					}
				}
			}
			
			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
				super.onError(ex, isOnCallback);
			}
		});
	}
	
	private OnCheckedChangeListener checkedChangeListener = new OnCheckedChangeListener() {
		
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			if(isChecked){
				ToastMessage.showMyToast(AgeActivity.this, "已开启");
			}else{
				ToastMessage.showMyToast(AgeActivity.this, "已关闭");
			}
		}
	};
	
	@Override
	protected void onStart() {
		super.onStart();
		if(null != spHelper && !TextUtils.isEmpty(memberId)){
			//Log.e("TAG", "onStart----AgeActivity前memberId===---"+memberId);
			userPhone = memberId;
			//Log.e("TAG", "onStart----AgeActivity后userPhone===---"+userPhone);
		}
	}
	
	@Override
	void setStatusBar(int statusBarColorRes) {
		super.setStatusBar(R.color.transparent_3);
	}
	
	
}
